using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace CustomControls.UserControls
{
    public partial class UserControlDefaultPath : UserControlBase
    {
        public UserControlDefaultPath()
        {
            InitializeComponent();
        }

        public override void Initialize()
        {
            IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            comboBoxPathFavoriteLeft.Items.Clear();
            comboBoxPathFavoriteRight.Items.Clear();

            //Loop through Favorites
            foreach (KeyValuePair<string, string> favoriteEntry in localSettings.FavoriteList)
            {
                string favoriteName = favoriteEntry.Key;

                comboBoxPathFavoriteLeft.Items.Add(favoriteName);
                comboBoxPathFavoriteRight.Items.Add(favoriteName);
            }

            //Set the DefaultPaths
            switch (localSettings.LeftDefaultFolderType)
            {
                case FolderType.SpecificFolder:
                    radioButtonSpecificDefaultLeft.Checked = true;
                    textBoxPathSpecificDirectoryLeft.Text = localSettings.LeftDefaultFolder;
                    break;
                case FolderType.FavoriteFolder:
                    radioButtonFavoriteDefaultLeft.Checked = true;
                    comboBoxPathFavoriteLeft.Text = localSettings.LeftDefaultFolder;
                    break;
                default:
                    radioButtonLastDefaultLeft.Checked = true;
                    break;
            } //End switch FolderType

            switch (localSettings.RightDefaultFolderType)
            {
                case FolderType.SpecificFolder:
                    radioButtonSpecificDefaultRight.Checked = true;
                    textBoxPathSpecificDirectoryRight.Text = localSettings.RightDefaultFolder;
                    break;
                case FolderType.FavoriteFolder:
                    radioButtonFavoriteDefaultRight.Checked = true;
                    comboBoxPathFavoriteRight.Text = localSettings.RightDefaultFolder;
                    break;
                default:
                    radioButtonLastDefaultRight.Checked = true;
                    break;
            } //End switch FolderType

            DirtyFlag = false;

        }

        private void buttonBrowseDirectoryLeft_Click(object argSender, EventArgs argEventArgs)
        {
            //TO DO:Open directory browser
            FolderBrowserDialog browseDefault = new FolderBrowserDialog
                {
                    ShowNewFolderButton = false,
                    RootFolder = Environment.SpecialFolder.DesktopDirectory
                };

            browseDefault.ShowDialog(this);
            textBoxPathSpecificDirectoryLeft.Text = browseDefault.SelectedPath;
        }

        private void buttonBrowseDirectoryRight_Click(object argSender, EventArgs argEventArgs)
        {
            //TO DO:Open directory browser
            FolderBrowserDialog browseDefault = new FolderBrowserDialog
                {
                    ShowNewFolderButton = false,
                    RootFolder = Environment.SpecialFolder.DesktopDirectory
                };

            browseDefault.ShowDialog(this);
            textBoxPathSpecificDirectoryRight.Text = browseDefault.SelectedPath;
        }

        private void comboBoxFavoriteLeft_SelectedIndexChanged(object argSender, EventArgs argEventArgs)
        {
            radioButtonFavoriteDefaultLeft.Checked = true;
            DirtyFlag = true;
        }

        private void comboBoxFavoriteRight_SelectedIndexChanged(object argSender, EventArgs argEventArgs)
        {
            radioButtonFavoriteDefaultRight.Checked = true;
            DirtyFlag = true;
        }

        private void textBoxSpecificDirectory_TextChanged(object argSender, EventArgs argEventArgs)
        {
            DirtyFlag = true;
        }

        private void radioButton_CheckedChanged(object argSender, EventArgs argEventArgs)
        {
            DirtyFlag = true;
        }

        public override void OnApply() //object sender, EventArgs argPaintArgs)
        {
            if (!DirtyFlag)
            {
                return;
            }

            IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            //Determine which method we are using
            if (radioButtonSpecificDefaultLeft.Checked)
            {
                localSettings.LeftDefaultFolderType = FolderType.SpecificFolder;
                localSettings.LeftDefaultFolder = textBoxPathSpecificDirectoryLeft.Text;
            }
            else if (radioButtonFavoriteDefaultLeft.Checked)
            {
                localSettings.LeftDefaultFolderType = FolderType.FavoriteFolder;
                localSettings.LeftDefaultFolder = comboBoxPathFavoriteLeft.Text;
            }
            else
            {
                localSettings.LeftDefaultFolderType = FolderType.LastFolder;
            }

            //Determine which method we are using
            if (radioButtonSpecificDefaultRight.Checked)
            {
                localSettings.RightDefaultFolderType = FolderType.SpecificFolder;
                localSettings.RightDefaultFolder = textBoxPathSpecificDirectoryRight.Text;
            }
            else if (radioButtonFavoriteDefaultRight.Checked)
            {
                localSettings.RightDefaultFolderType = FolderType.FavoriteFolder;
                localSettings.RightDefaultFolder = comboBoxPathFavoriteRight.Text;
            }
            else
            {
                localSettings.RightDefaultFolderType = FolderType.LastFolder;
            }

            localSettings.Save();
            DirtyFlag = false;

        }

    }
}
